package com.ruoyi.web.controller.feishu;

import com.lark.oapi.service.authen.v1.model.GetUserInfoResp;
import com.ruoyi.web.controller.tool.FeishuOfficeUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 注意：
 * 当前在用的中台BI
 * 2024-04-24 记录
 */
@RestController
@RequestMapping("/fsc/sanfang")
public class GbFeishuToSanfangController {

    @Value("${feishu.bi.url}")
    private String url;

    /**
     * 飞书配置地址 http://192.168.180.120:8088/bi/Viewer?sysFlag=feishu
     * BI端设置回调地址 http://192.168.99.66:30054/fsc/sanfang/bi/tokenCheck?token=u-7OkD.w5Lh0DWhH8vNsiw0i551wqg05fzN0G040Cw08k1
     *
     * @return 问题：token参数能否动态传过来
     */
    @RequestMapping(value = "/bi/tokenCheck")
    public String tokenCheckGet(@RequestParam("token") String token) throws Exception {
        System.err.println("==============BI单点登录检验==============" + token);

        GetUserInfoResp userInfoResp = FeishuOfficeUtil.getUserInfoResp(token);
        String employeeNo = userInfoResp.getData().getEmployeeNo();
        // 校验token是否合法
        StringBuilder responseStr = new StringBuilder();
        if (token == null || "".equals(token.trim())) {
            //  客户自己的错误处理逻辑
            return responseStr.toString();
        } else {
            //token验证通过就返回当前登录用户。对应永洪系统中的用户名。
            responseStr.append("{");
            responseStr.append("\"result\":\"success\",");
            responseStr.append("\"userId\":\"" + employeeNo + "\"");
            responseStr.append("}");
        }
        System.err.println("responseStr:" + responseStr.toString());
        return responseStr.toString();
    }

    @GetMapping("/getToken")
    public Map<String,Object> getToken(@RequestParam("code") String code) throws Exception {
        String token = FeishuOfficeUtil.singleLogin(code);

        Map<String,Object> m = new HashMap<>();
        m.put("url",url);
        m.put("token",token);

        System.err.println("BI地址："+url);
        System.err.println("BI地址token："+token);
        return m;
    }
}
